package vn.co.thn.android.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class TextViewCustom extends TextView {
	private int mMaxLength;
	private CharSequence mTextEnd;
	private int mTextType;
	//
	TextLinkClickListener mListener;

	public TextViewCustom(Context context) {
		super(context);
	}

	public TextViewCustom(Context context, AttributeSet attrs) {
		super(context, attrs);
		// if (!isInEditMode())
		init(context, attrs);
	}

//	public TextViewCustom(Context context, AttributeSet attrs, int defStyle) {
//		super(context, attrs, defStyle);
//		// if (!isInEditMode())
//		init(context, attrs);
//	}

	private void init(Context context, AttributeSet attrs) {
		TypedArray a = null;
		try {
			a = context.obtainStyledAttributes(attrs,
					R.styleable.TextViewCustom);
			mMaxLength = a.getInteger(R.styleable.TextViewCustom_maxlength, 0);
			mTextEnd = a.getString(R.styleable.TextViewCustom_textend);
			if (TextUtils.isEmpty(mTextEnd)) {
				mTextEnd = "...";
			}
			mTextType = a.getInt(R.styleable.TextViewCustom_TextType, 0);
		} finally {
			if (a != null)
				a.recycle();
		}
		setText(getText());
	}

	@Override
	public void setText(CharSequence text, BufferType type) {
		
		if (mTextType == 3) {
			if (mMaxLength > 0 && text.length() > mMaxLength) {

				text = text.subSequence(0, mMaxLength) + mTextEnd.toString();
				int start = mMaxLength;
				int end = mMaxLength + mTextEnd.length();
				SpannableString linkableText = new SpannableString(text);
				Hyperlink spec = new Hyperlink();
				spec.textSpan = linkableText.subSequence(start, end);
				spec.span = new InternalURLSpan(spec.textSpan.toString());
				spec.start = start;
				spec.end = end;

				linkableText.setSpan(spec.span, spec.start, spec.end,
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				super.setText(linkableText, type);
				return;
			}
		}
		if (mTextType == 2) {
			SpannableString linkableText = new SpannableString(text);
			Hyperlink spec = new Hyperlink();
			spec.textSpan = linkableText.subSequence(0, text.length());
			spec.span = new InternalURLSpan(spec.textSpan.toString());
			spec.start = 0;
			spec.end = text.length();

			linkableText.setSpan(spec.span, spec.start, spec.end,
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			super.setText(linkableText, type);
			return;
		}
		if (mTextType == 1) {
			if (mMaxLength > 0 && text.length() > mMaxLength) {

				text = text.subSequence(0, mMaxLength) + mTextEnd.toString();
				super.setText(text, type);
				return;
			}
		}
		super.setText(text, type);
	}

	public void setOnTextLinkClickListener(TextLinkClickListener newListener) {
		mListener = newListener;
	}

	//
	public interface TextLinkClickListener {
		public void onTextLinkClick(View textView, String clickedString);

	}

	public class InternalURLSpan extends ClickableSpan {
		private String clickedSpan;

		public InternalURLSpan(String clickedString) {
			clickedSpan = clickedString;
		}

		@Override
		public void onClick(View textView) {
			if (mListener != null)
				mListener.onTextLinkClick(textView, clickedSpan);
		}
	}

	class Hyperlink {
		CharSequence textSpan;
		InternalURLSpan span;
		int start;
		int end;
	}
}
